package model

import (
	"fmt"
	"sync"
	"time"
)

type ScheduledTask struct {
	timer     *time.Timer
	Nums      int
	Type      string
	size      int
	Chan      chan bool
	IsClosed  bool
	ModelName string
	Content   string
	Stream    bool
}

func NewScheduledTask(size, times, nums int, t, modelName, content string, stream bool) (st *ScheduledTask) {
	st = new(ScheduledTask)
	st.size = size
	st.Nums = nums
	st.Type = t
	st.timer = time.NewTimer(time.Duration(times) * time.Second)
	st.Chan = make(chan bool, st.size)
	st.ModelName = modelName
	st.Content = content
	st.Stream = stream
	fmt.Println("--------任务开始--------")
	for i := 0; i < st.size; i++ {
		st.Chan <- true
	}
	return st
}

func (st *ScheduledTask) ListenTime(wg *sync.WaitGroup) {
	defer wg.Done()
	for {
		<-st.timer.C
		st.IsClosed = true
		close(st.Chan)
		break
	}
	fmt.Println("--------- 定时任务结束  ----------")
}

func (st *ScheduledTask) ListenNum(wg *sync.WaitGroup) {
	defer wg.Done()
	for {
		if st.Nums == 0 {
			st.IsClosed = true
			close(st.Chan)
			break
		}
	}
	fmt.Println("--------- 定时任务结束  ----------")
}

func (st *ScheduledTask) GetSize() int {
	return st.size
}
